#include "mypushbutton.h"
#include<QDebug>
#include<QPropertyAnimation>
//myPushButton::myPushButton(QWidget *parent)
//    : QPushButton{parent}
//{

//}
myPushButton::myPushButton (QString normalImage,QString pressImage)
{
    this->normalImagePath=normalImage;
    this->pressImagePath=pressImage;

    QPixmap pix;
    bool ret = pix.load(normalImage);
    if(!ret)
    {
        qDebug()<<"图片加载失败";
        return;

    }
    //图片固定大小设定
    this->setFixedSize(pix.width(),pix.height());

    //设置不规则图片
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图标
    this->setIcon(pix);

    //图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));

}




void myPushButton::zoomdown(){

    //创建动画对象
    QPropertyAnimation *strdown = new QPropertyAnimation (this,"geometry");
    //设置动画时间间隔
    strdown->setDuration(200);
    //起始位置
    strdown->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //结束位置
    strdown->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置曲线
    strdown->setEasingCurve(QEasingCurve::OutBounce);
    //执行动画
    strdown->start();
}

void myPushButton::zoomup(){

    //创建动画对象
    QPropertyAnimation *strup = new QPropertyAnimation (this,"geometry");
    //设置动画时间间隔
    strup->setDuration(200);
    //起始位置
    strup->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //结束位置
    strup->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置曲线
    strup->setEasingCurve(QEasingCurve::OutBounce);
    //执行动画
    strup->start();
}
void myPushButton::mousePressEvent(QMouseEvent *e)
{
    if(this->pressImagePath != "")//传入不为空，代表需要切换
    {
        QPixmap pix;
        bool ret = pix.load(this->pressImagePath);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;

        }
        //图片固定大小设定
        this->setFixedSize(pix.width(),pix.height());

        //设置不规则图片
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));

    }

    //让父类执行其他内容
    return QPushButton::mousePressEvent(e);
}

void myPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(this->pressImagePath != "")//传入不为空，代表需要切换
    {
        QPixmap pix;
        bool ret = pix.load(this->normalImagePath);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;

        }
        //图片固定大小设定
        this->setFixedSize(pix.width(),pix.height());

        //设置不规则图片
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));

    }
    //让父类执行其他内容
    return QPushButton::mouseReleaseEvent(e);

}







